load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;------------------------------------------------------------------
;read and combine data
;------------------------------------------------------------------
;

indir = "DATA/FIG_2/"

infile := addfile(indir+"D6_Drought_NOTC_TC_1980_2020.nc","r")
Dn=infile->Ndp
Dd=infile->Ddp  ;NOTC-TC (LONG)
Dn1=Dn*-1
Dd1=Dd*-1
copy_VarMeta(Dn,Dn1)
copy_VarMeta(Dd,Dd1)
Dn=Dn1
Dd=Dd1
;----------------------------------------------------------
; Plot map
;----------------------------------------------------------


plot = new((/2/),"graphic")
type = (/"eps","png"/)

files = "FIG_MAIN/Fig_2"

do m = 0,1

  wks = gsn_open_wks(type(m),files)
   
   gsn_define_colormap(wks,"BlueDarkRed18")   ;    "precip3_16lev" "MPL_YlOrRd"
   gsn_reverse_colormap(wks)
;"GSFC_landsat_udf_density"
  res = True
  res@gsnDraw = False
  res@gsnFrame = False
  res@gsnMaximize = False
  res@vpXF = 0.05               ; default is 0.2 (aspect ratio)
  res@vpYF = 0.96              ; default is 0.8
  res@vpHeightF = 0.29             ; default is 0.6
  res@vpWidthF = 0.8

  res@mpMinLatF           = -60 ;-89.75         ; map area
  res@mpMaxLatF           = 85  ;89.75        ; latitudes
  res@mpMinLonF           = -179.75       ; and
  res@mpMaxLonF           = 179.75       ; longitudes

  res@mpPerimOn = False
  res@mpPerimLineThicknessF = 3.0
  res@mpDataBaseVersion     = "MediumRes"
  res@mpNationalLineColor = "gray20"
  res@mpGeophysicalLineColor = "gray20"
  res@mpOutlineOn = True
  res@mpOutlineBoundarySets = "National"
  
  ;------------JK  Robinson Projection
  res@mpProjection = "Robinson"
  res@mpGridAndLimbOn = True
  res@mpGridLatSpacingF = 30.
  res@mpGridLonSpacingF = 60.
  res@gsnRightString = ""
;------------JK
  
  
 
  res@cnFillOn = True                    ; no map fill
  res@cnFillMode = "Meshfill"
  res@cnFillOpacityF = 1.0

  res@cnLinesOn = False
  res@cnLineLabelsOn = False
  res@gsnAddCyclic = False
  res@tmXBLabelFontHeightF = 0.008
  res@tmYLLabelFontHeightF = 0.008
  
  res@lbTitleFontHeightF = 0.013
  res@lbLabelFontHeightF = 0.011
  res@tiMainFontHeightF = 0.016
  res@gsnLeftStringFontHeightF = 0.02
  res@mpFillOn = False
  
  ;;label

  res@lbAutoManage       = False          ; Necessary to control sizes
  
     res@lbLabelBarOn               = False

    ; Set resources for when we turn on labelbar later
    res@lbTitleFontHeightF         = .010                 ; make title smaller
    res@lbLabelAutoStride          =True
    res@lbLabelFontHeightF         = 0.01   ;0.011
    res@pmLabelBarHeightF          = 0.2  ;0.05
    res@lbBoxEndCapStyle           =  "TriangleBothEnds";"TriangleLowEnd"  ;
    res@lbOrientation              =   "Horizontal"        ; orientation  "Vertical" 
    res@lbLabelStride              = 2
    ;res@lbTitleAngleF = 270.0
    res@lbTitleDirection = "Across"
    ;res@lbTitlePosition = "right"
    res@pmLabelBarWidthF      = 0.4              ; default is shorter
    res@pmLabelBarHeightF     = 0.06              ; default is taller
    res@lbPerimOn             = False             ; default has box
    
  res2=res
  
  res2@tmXBLabelsOn         = False              ; do not draw bottom labels
  res2@lbTitleString = "[%]"
  res2@gsnLeftString = "A) Change in Extreme Long- Term (D6) Drought Frequency"
  res2@gsnLeftStringFontHeightF = 0.014

  ;res2@pmLabelBarOrthogonalPosF = 0.05  ; JK

  clevs := fspan(-60,60,13)  ; JK: Please change the min-max range for TC Event count
  res2@gsnSpreadColors = True
  res2@cnLevelSelectionMode= "ExplicitLevels"
  res2@cnLevels := clevs
  plot(0) = gsn_csm_contour_map(wks, Dn, res2) ; JK: Please change the data for TC Event Counts
 
   res2@lbLabelBarOn               = True
  ;gsn_define_colormap(wks,"sunshine_diff_12lev") 
  ;clevs := fspan(-300,300,9)
  res2@vpXF = 0.05               ; default is 0.2 (aspect ratio)
  res2@vpYF = 0.62              ; default is 0.8
  res2@lbTitleString = "[%]"
  res2@gsnLeftString = "B) Change in Extreme Long- Term (D6) Drought Duration"
  ;res2@cnLevels := clevs
  res4=res2
  res4@tmXBLabelsOn         = True  
  res4@tmYLLabelsOn         = True 
  
  plot(1) = gsn_csm_contour_map(wks, Dd, res4)
  

  
  pgres = True
  pgres@gsEdgesOn = True
  pgres@gsFillColor = -1
  pgres@gsEdgeColor = 1
  pgres@gsEdgeDashPattern = 0
  pgres@gsEdgeThicknessF = 3.0
  dum1 = gsn_add_polygon(wks,plot(0),(/-130,-50,-50,-130/),(/5, 5, 50, 50/),pgres)
  dum2 = gsn_add_polygon(wks,plot(0),(/100,160,160,100/),(/5, 5, 50, 50/),pgres)
  dum3 = gsn_add_polygon(wks,plot(0),(/40,100,100,40/),(/5, 5, 50, 50/),pgres)
  dum4 = gsn_add_polygon(wks,plot(0),(/100,179,179,100/),(/-5, -5, -50, -50/),pgres)
  dum5 = gsn_add_polygon(wks,plot(0),(/25,65,65,25/),(/-5, -5, -50, -50/),pgres)

  txres = True
  txres@txJust = "CenterLeft"
  txres@txFontColor = "darkviolet"
  txres@txAngleF = 0.0
  txres@txFontThicknessF = 4.0
  txres@txFontHeightF = 0.008

  regs = (/"NCA", "EAS", "SWA", "OCE", "EAF"/)

  tdum1 = gsn_add_text(wks,plot(0),regs(0),-128,10,txres)
  tdum2 = gsn_add_text(wks,plot(0),regs(1),139,10,txres)
  tdum3 = gsn_add_text(wks,plot(0),regs(2),75,45,txres)
  tdum4 = gsn_add_text(wks,plot(0),regs(3),102,-44,txres)
  tdum5 = gsn_add_text(wks,plot(0),regs(4),44,-44,txres)


    
    
  draw(plot)
  frame(wks)

end do

system("convert -density 300 "+ files+".eps " +files+".png")



